package com.rachel.studyapp.mvp.model;

import android.content.Context;
import android.util.Log;

import com.rachel.okhttplib.OkHttpCommonClient;
import com.rachel.okhttplib.callback.JsonResponse;
import com.rachel.studyapp.json.funjson.TodayBean;
import com.rachel.studyapp.mvp.ContractUtils;

import java.util.Calendar;

/**
 * Created by zhengshaorui on 2017/8/30.
 */

public class FunModel {
    private static final String TAG = "zsr";
    //开眼首页 url
    private static final String ROOT_URL = "http://baobab.kaiyanapp.com/api/v4/tabs/selected";
    //历史上的今天 month=11&day=1
    private static final String TODAYHISTORY = "http://api.juheapi.com/japi/toh?key=3f7112ffcd956b17d829d639a86464b3&v=1.0&";
    private OkHttpCommonClient mClient = OkHttpCommonClient.getInstance();

    private ContractUtils.IFunPresent mListener;
    private Context mContext;
    public FunModel(ContractUtils.IFunPresent listener, Context context) {
        this.mListener = listener;
        mContext = context;
        getJsonBean(TODAYHISTORY);
    }

    public void getJsonBean(String url){
        Calendar calendar = Calendar.getInstance();
        String path = url+"month="+(calendar.get(Calendar.MONTH)+1)+"&"+"day="+calendar.get(Calendar.DAY_OF_MONTH);
        Log.d(TAG, "getJsonBean: "+path);
        mClient.getBuilder()
                .url(path)
                .builder()
                .enqueue(new JsonResponse(TodayBean.class) {
                    @Override
                    public void onSuccess(Object response) {
                        TodayBean bean = (TodayBean) response;
                        Log.d(TAG, "onSuccess: "+bean.getError_code());
                        mListener.getJsonDataSuccess(bean);
                    }

                    @Override
                    public void onFailure(Object errorObj) {
                        mListener.onFailure(errorObj);
                    }
                });


       /* try {
            InputStream is = mContext.getResources().getAssets().open("fun_data.json");
            StringBuffer sb = new StringBuffer();
            int len = -1;
            byte[] buf = new byte[1024];
            while ( (len = is.read(buf)) != -1 ){
                sb.append(new String(buf,0,len));
            }
            new OpenEyeAync().execute(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }*/



    }
    /*class OpenEyeAync extends AsyncTask<String,Integer,FunJson>{

        @Override
        protected FunJson doInBackground(String... params) {
            Gson gson = new Gson();
            FunJson bean = gson.fromJson(params[0],FunJson.class);
            Log.d(TAG, "doInBackground: "+bean.getItemList().get(1).getData().getTitle());
            return bean;
        }

        @Override
        protected void onPostExecute(FunJson funJson) {
            super.onPostExecute(funJson);
            if (funJson != null){
                mListener.getJsonDataSuccess(funJson);
            }
        }
    }*/
}
